/**
 * 获取推荐页数据并存入数据库
 */
const request=require("request");
const fs=require("fs");
const {recommendTable}=require("./recommendTable")

request(
    {
        method:"GET",
        url:"https://u.y.qq.com/cgi-bin/musicu.fcg",
        qs:{
            cgiKey: "GetHomePage",
            _: "1584671684044",
            data: `{"comm":{"g_tk":5381,"uin":"","format":"json","inCharset":"utf-8","outCharset":"utf-8","notice":0,"platform":"h5","needNewCode":1},"MusicHallHomePage":{"module":"music.musicHall.MusicHallPlatform","method":"MobileWebHome","param":{"ShelfId":[101,102,161]}},"hotkey":{"module":"tencent_musicsoso_hotkey.HotkeyService","method":"GetHotkeyForQQMusicMobile","param":{"remoteplace":"txt.miniapp.wxada7aab80ba27074","searchid":"1559616839293"}}}`
        }
    },async(err,res,body)=>{
        await recommendTable.deleteMany({});
        let data=JSON.parse(body).MusicHallHomePage.data.v_shelf //获取所有的推荐分区信息
        // console.log(data)
        data.forEach((item)=>{
            let category=item.title_template;
            let categoryList=item.v_niche[0].v_card; //获取详情列表
            let arr=[];
            categoryList.forEach((list)=>{
                if(list.id){
                    arr.push({
                        id:list.id,
                        title:list.title,
                        cover:list.cover
                    })
                }
            })
            recommendTable.create({
                category:category,
                categoryList:arr
            }).then(()=>{
                console.log("数据库成功")
            }).catch(()=>{
                console.log("失败")
            })
        })
    })